
import axios from 'axios';
import qs from 'qs';

const headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

export default {
    async getSubmit (serverURL,paramObj){
    let response = await axios({
        method:"get",
        url:serverURL,
        params:paramObj,
        paramsSerializer: function(params) {
            return qs.stringify(params, {arrayFormat: 'repeat'})
        }
    })
    return response.data;
    },
    
   async postSubmit(serverURL,paramObj)  {
    let response = await axios({
        headers,
        method:"post",
        url:serverURL,
        data:qs.stringify(paramObj, {arrayFormat: 'repeat'})
    })
    return response.data;
    },

    async uploadSubmit (submitURL,param) {
    
        let formData = new FormData();

        for(var fieldName in param){
            formData.append(fieldName, param[fieldName]);
        }
        let config = {
            headers: {
            'Content-Type': 'multipart/form-data'
            }
        }
        var response = await axios.post(submitURL, formData, config);
        return response.data;
  
    }

}

export const login = async (username,password) => {
    let response = await axios({
        headers,
        method:"post",
        url:"/api/users/login",
        data:qs.stringify({
            username,password
        })
    })
    return response.data;
}

export const showAll = async (page=1,size=5) => {
    console.log(page,size)
    let response = await axios({
        method:"get",
        url:"/api/comments/showAll",
        params:{page,size}
    })
    return response.data;
}